
"""The app for hosting the master server."""

import logging
import os
import tornado.ioloop
from gaweb import common
from gaweb import consts
from gaweb.master import helpers
from gaweb.master import model
from gaweb.master.handlers import ajax_handler
from gaweb.master.handlers import main_handler 
from gaweb.master.handlers import template_handler


def main():
    config = helpers.read_config()           # let error propogate
    common.setup_logging(config)

    logging.info('Starting up')
    
    master = model.Master()
    settings = {
        'static_path': os.path.join(os.path.dirname(__file__), 'static')
    }
    application = tornado.web.Application([
            (r'/', main_handler.MainHandler),
            (r'/ajax', ajax_handler.AjaxHandler, dict(master=master)),
            (r'/template', template_handler.TemplateHandler, dict(master=master)),
    ], **settings)
    
    application.listen(consts.MASTER_LISTEN_PORT)
    tornado.ioloop.IOLoop.instance().start()
